<template>
	<view class="container box">
		<view class="main">
			<view class="set">
				<uni-icons @click="$go2Page('sport')" class="set-right" type="gear" size="30"></uni-icons>
			</view>
		</view>
		<scroll-view scroll-y="true" class="contents">
			<view v-if="sports">
				<template v-for="(item,index) in sports">
					<uni-row v-if="index%2==0" :gutter="20">
						<uni-col :span="12">
							<view class="sport-event">
								<uni-icons :color="item.fav?'red':'#6495ED'" @click="favFn(index)"
									:type="item.fav?'star-filled':'star'" size="30" class="icon-fav"></uni-icons>
								<image :src="item.img" mode="widthFix" class="event-img" @click="goTrain(item.id)">
								</image>
								{{item.event}}
							</view>
						</uni-col>
						<uni-col :span="12" v-if="index+1<sports.length">
							<view class="sport-event">
								<uni-icons :color="sports[index+1].fav?'red':'#6495ED'" @click="favFn(index+1)"
									:type="sports[index+1].fav?'star-filled':'star'" size="30"
									class="icon-fav"></uni-icons>
								<image :src="sports[index+1].img" mode="widthFix" class="event-img"
									@click="goTrain(sports[index+1].id) ">
								</image>
								{{sports[index+1].event}}
							</view>
						</uni-col>
					</uni-row>
				</template>
			</view>

		</scroll-view>
		<view :class="isRecord?'record-hide':'record'">
			<view class="record-item" @click.native="changeShow">
				记录
			</view>

			<view class="record-item" v-if="!isRecord" @click="$go2Page('healthRecord')">
				健康记录
			</view>
			<view class="record-item" v-if="!isRecord" @click="$go2Page('traindDetail')">
				运动记录
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getSportFavCommonData,
		check,
		addData,
		remove
	} from '../../../database/index.js'
	import {
		onBeforeMount,
		onMounted
	} from "vue";
	export default {
		data() {
			return {
				sports: '',
				isRecord: true
			}
		},
		async onPullDownRefresh() {
			this.sports = await getSportFavCommonData()
		},
		async onLoad() {
			this.sports = await getSportFavCommonData()
		},
		methods: {
			async favFn(index) {
				let res = await remove('sportCollect', {
					sport_Id: this.sports.id,
					sport_collector: this.$store.getters.course_collector
				})
				if (res) {
					this.$showMsg(`取消收藏`)
					this.sports.splice(index, 1)
				} else {
					this.$showMsg(`取消收藏失败`)
				}
			},


			goTrain(sport_id) {
				setTimeout(() => {
					uni.navigateTo({
						url: `/pages/sport/training/training?id=${sport_id}`,
					})
				}, 1 * 1000)
			},
			changeShow() {
				this.isRecord = !this.isRecord
			}
		}
	}
</script>
<style lang="scss" scoped>
	.set {
		text-align: right;
	}

	.record {
		left: 30rpx;
		width: calc(100% - 40rpx);
		bottom: 20rpx;
		position: fixed;
		display: flex;
		width: 180rpx;

		.record-item {
			background-color: rgba(65, 105, 225, .6);
			color: white;
			text-align: center;
			padding: 25rpx 50rpx;
			border-right: 1rpx solid white;

			&:first-child {
				background-color: rgba(65, 105, 225, .8);
			}

			&:last-child {
				border-right: none;
			}
		}
	}


	.record-hide {
		@extend .record;
		border-right: none;
	}

	.box {
		flex-direction: column;
		display: flex;
		color: black;
		position: relative;
		padding-top: 5rpx;

		.contents {
			margin-top: 10rpx;
			flex: 1;

			.sport-event {
				text-align: center;
				background-color: white;
				margin-bottom: 20rpx;
				padding: 20rpx 0;
				position: relative;

				.icon-fav {
					position: absolute;
					right: 10rpx;
					top: 10rpx;
				}

				.event-img {
					display: block;
					margin: 0 auto;
					width: 60rpx;
					margin-bottom: 10rpx;
				}
			}
		}
	}
</style>